<?php

require_once(sfConfig::get('sf_plugins_dir') . '/sfDoctrineGuardPlugin/modules/sfGuardAuth/lib/BasesfGuardAuthActions.class.php');

class sfGuardAuthActions extends BasesfGuardAuthActions {

    public function executeChange(sfWebRequest $request) {
        $this->form = new ChangeForm($this->getUser()->getGuardUser());

        $this->parametros = array();
        $this->parametros['form'] = $this->form;
        $this->parametros['user'] = $this->getUser()->getGuardUser();

        if ($request->isMethod('put') || $request->isMethod('post')) {
            $this->processForm($request, $this->form);
        }
        $this->setLayout('layout');
    }

    public function executeSignin($request) {
        if (!$request->isXmlHttpRequest()) {
            $user = $this->getUser();
            if ($user->isAuthenticated()) {
                return $this->redirect('@homepage');
            }

            $this->form = new formSignin();

            if ($request->isMethod('post')) {
                $this->form->bind($request->getParameter('signin'));
                if ($this->form->isValid()) {
                    $values = $this->form->getValues();
                    $this->getUser()->signin($values['user'], array_key_exists('remember', $values) ? $values['remember'] : false);

                    // always redirect to a URL set in app.yml
                    // or to the referer
                    // or to the homepage
                    $signinUrl = sfConfig::get('app_sf_guard_plugin_success_signin_url', $user->getReferer($request->getReferer()));

                    return $this->redirect('' != $signinUrl ? $signinUrl : '@homepage');
                } else {
                    $errors = $this->form->getGlobalErrors();
                    foreach ($errors as $error) {
                        if ($error == DesarrolloBarrios::getCSRFMessage()) {
                            $this->getUser()->setFlash('error', sprintf('Por razones de seguirdad, ingrese su usuario y clave nuevamente'));
                            return $this->redirect('@homepage');
                        }
                    }
                }
            } else {
                // if we have been forwarded, then the referer is the current URL
                // if not, this is the referer of the current request
                $user->setReferer($this->getContext()->getActionStack()->getSize() > 1 ? $request->getUri() : $request->getReferer());

                $module = sfConfig::get('sf_login_module');
                if ($this->getModuleName() != $module) {
                    return $this->redirect($module . '/' . sfConfig::get('sf_login_action'));
                }

                $this->getResponse()->setStatusCode(401);
            }
        }
        else
            $this->setTemplate('goToHomepage');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $sf_guard_user = $form->save();

            $this->redirect('user/show?id=' . $sf_guard_user->getId());
        }
    }

}
